#cisco netmiko XR
Explore tagged Tumblr posts
Text
Ease in Automation: Convert Router Output to Json Format
Ease in Automation: Convert Router Output to Json Format
In this article we will be talking about converting the output from Cisco XR into a Json File for further utilization or automation. The following code can be used and can be redesigned to work with several other commands and output in other variants of Cisco XE, XR etc., as well as other vendor devices. Requirements: > Python3 > Netmiko > TextFSM > Json Netmiko library is one of the most useful…
View On WordPress
#automating outputs to json#cisco netmiko XR#cisco XR#CiscoXE#converting output from router to json#IOS#json#multi-list to json#netmiko#regex usage in cisco XR#show bgp summary#textfsm#textfsm usage#textfsm usgae#XE#XR
0 notes
Text
New Coder: Real World Code Development
In this post, part of a miniseries on coding for non-coders, I thought it might be interesting to look at a real-world example of breaking a task down for automation. I won't be digging hard into the actual code but instead looking at how the task could be approached and turned into a sequence of events that will take a sad task and transform it into a happy one.
The Task - Deploying a New VLAN
Deploying a new VLAN is simple enough, but in my environment it means connecting to around 20 fabric switches to build the VLAN. I suppose one solution would be to use an Ethernet fabric that had its own unified control plane, but ripping out my Cisco FabricPath™ switches would take a while, so let's just put that aside for the moment.
When a new VLAN is deployed, it almost always also requires that a layer 3 (IP) gateway with HSRP is created on the routers and that VLAN needs to be trunked from the fabric edge to the routers. If I can automate this process, for every VLAN I deploy, I can avoid logging in to 22 devices by hand, and I can also hopefully complete the task significantly faster.
Putting this together, I now have a list of three main steps I need to accomplish:
Create the VLAN on every FabricPath switch
Trunk the VLAN from the edge switches to the router
Create the L3 interface on the routers, and configure HSRP
Don't Reinvent the Wheel
Much in the same way that one uses modules when coding to avoid rewriting something that has been created already, I believe that the same logic applies to automation. For example, I run Cisco Data Center Network Manager (DCNM) to manage my Ethernet fabric. DCNM has the capability to deploy changes (it calls them Templates) to the fabric on demand. The implementation of this feature involves DCNM creating an SSH session to the device and configuring it just like a real user would. I could, of course, implement the same functionality for myself in my language of choice, but why would I? Cisco has spent time making the deployment process as bulletproof as possible; DCNM recognizes error messages and can deal with them. DCNM also has the logic built in to configure all the switches in parallel, and in the event of an error on one switch, to either roll back that switch alone or all switches in the change. I don't want to have to figure all that out for myself when DCNM already does it.
For the moment, therefore, I will use DCNM to deploy the VLAN configurations to my 20 switches. Ultimately it might be better if I had full control and no dependency on a third-party product, but in terms of achieving the goal rapidly, this works for me. To assist with trunking VLANs toward the routers, in my environment the edge switches facing the routers have a unique name structure, so I was also able to tweak the DCNM template so that if it detects that it is configuring one of those switches, it also adds the VLANs to the trunked list on the relevant router uplinks. Again, that's one less task I'll have to do in my code.
Similarly, to configure the routers (IOS XR-based), I could write a Python script based on the Paramiko SSH library, or use the Pexpect library to launch ssh and control the program's actions based on what it sees in the session. Alternatively, I could use NetMiko which already understands how to connect to an IOS XR router and interact with it. The latter choice seems like it's preferable, if for no other reason than to speed up development.
Creating the VLAN
DCNM has a REST API through which I can trigger a template deployment. All I need is a VLAN number and an optional description, and I can feed that information to DCNM and let it run. First, though, I need the list of devices on which to apply the configuration template. This information can be retrieved using another REST API call. I can then process the list, apply the VLAN/Description to each item and submit the configuration "job." After submitting the request, assuming success, DCNM will return the JobID that was created. That's handy because it will be necessary to keep checking the status of that JobID afterward to see if it succeeded. So here are the steps so far:
Get VLAN ID and VLAN Description from user
Retrieve list of devices to which the template should be applied
Request a configuration job
Request job status until it has some kind of resolution (Success, Failed, etc)
Sound good? Wait; the script needs to login as well. In the DCNM REST API that means authenticating to a particular URL, receiving a token (a string of characters), then using that token as a cookie in all future requests within that session. Also, as a good citizen, the script should logout after completing its requests too, so the list now reads:
Get VLAN ID and VLAN Description from user
Authenticate to DCNM and extract session token
Retrieve list of devices to which the template should be applied
Request a configuration job
Request job status until it has some kind of resolution (Success, Failed, etc)
Log out of DCNM
That should work for the VLAN creation but I'm also missing a crucial step which is to sanitize and validate the inputs provided to the script. I need to ensure, for example, that:
VLAN ID is in the range 1-4094, but for legacy Cisco purposes perhaps, does not include 1002-1005
VLAN Description must be 63 characters or less, and the rules I want to apply will only allow [a-z], [A-Z], [0-9], dash [-] and underscore [_]; no spaces and odd characters
Maybe the final list looks like this then:
Get VLAN ID and VLAN Description from user
Confirm that VLANID and VLAN Description are valid
Authenticate to DCNM and extract session token
Retrieve list of devices to which the template should be applied
Request a configuration job
Request job status until it has some kind of resolution (Success, Failed, etc)
Log out of DCNM
Configuring IOS XR
In this example, I'll use Python+NetMiko to do the hard work for me. My inputs are going to be:
IPv4 Subnet and prefix length
IPv6 Subnet and prefix length
VLAN ID
L3 Interface Description
As before, I will sanity check the data provided to ensure that the IPs are valid. I have found that IOS XR's configuration for HSRP, while totally logical and elegantly hierarchical, is a bit of a mouthful to type out, so to speak, and as such it is great to have a script take the basic information like a subnet, and apply some standard rules to it (e.g. the 2nd IP is the HSRP gateway, e.g. .1 on a /24 subnet), the next address up (e.g. .2) would be on the A router, and .3 would be on the B router. For my HSRP group number, I use the VLAN ID. The subinterface number where I'll be configuring layer 3 will match the VLAN ID also, and with that information I can also configure the HSRP BFD peer between the routers too. By applying some simple standardized templating of the configuration, I can take a bare minimum of information from the user and create configurations which would take much longer to create manually and quite often (based on my own experience) would have mistakes in it.
The process then might look like this:
Get IPv4 subnet, IPv6 subnet, VLAN ID and L3 interface description from user
Confirm that IPv4 subnet, IPv6 subnet, VLANID and interface description are valid
Generate templated configuration for the A and B routers
Create session to A router and authenticate
Take a snapshot of the configuration
Apply changes (check for errors)
Assuming success, logout
Rinse and repeat for B router
Breaking Up is Easy
Note that the sequences of actions above have been created without requiring any coding. Implementation can come next, in the preferred language, but if we don't have an idea of where we're going, especially as a new coder, it's likely that the project will go wrong very quickly.
For implementation, I now have a list of tasks which I can attack, to some degree, separately from one another; each one is a kind of milestone. Looking at the DCNM process again:
Get VLAN ID and VLAN Description from user
Perhaps this data comes from a web page but for the purposes of my script, I will assume that these values are provided as arguments to the script. For reference, an argument is anything that comes after the name of the script when you type it on the command line, e.g. in the command, sayhello.py John the program sayhello.py would see one argument, with a value of John.
Confirm that VLANID and VLAN Description are valid
This sounds like a perfect opportunity to write a function/subroutine which can take a VLAN ID as its own argument, and will return a boolean (true/false) value indicating whether or not the VLAN ID is valid. Similarly, a function could be written for the description, either to enforce the allowed characters by removing anything that doesn't match, or by simply validating whether what's provided meets the criteria or not. These may be useful in other scripts later too, so writing a simple function now may save time later on.
Authenticate to DCNM and extract session token
Retrieve list of devices to which the template should be applied
Request a configuration job
Request job status until it has some kind of resolution (Success, Failed, etc)
Log out of DCNM
These five actions are all really the same kind of thing. For each one, some data will be sent to a REST API, and something will be returned to the script by the REST API. The process of submitting to the REST API only requires a few pieces of information:
What kind of HTML request is it? GET / POST / etc?
What is the URL?
What data needs to be sent, if any, to the URL?
How to process the data returned. (What format is it in?)
It should be possible to write some functions to handle GET and POST requests so that it's not necessary to repeat the HTTP request code every time it's needed. The idea is not to repeat code multiple times if it can be more simply put in a single function and called from many places. This also means that fixing a bug in that code only requires it to be fixed in one place.
For the IOS XR configuration, each step can be processed in a similar fashion, creating what are hopefully more manageable chunks of code to create and test.
Achieving Coding Goals
I really do believe that sometimes coders want to jump right into the coding itself before taking the time to think through how the code might actually work, and what the needs will be. In the example above, I've run through taking a single large task (Create a VLAN on 20 devices and configure two attached routers with an L3 interface and HSRP) which might seem rather daunting at first, and breaking it down into smaller functional pieces so that a) it's clearer how the code will work, and in what order; and b) each small piece of code is now a more achievable task. I'd be interested to know if you as a reader feel that the task lists, while daunting in terms of length, perhaps, seemed more accomplishable from a coding perspective than just the project headline. To me, at least, they absolutely are.
I said I wouldn't dig into the actual code, and I'll keep that promise. Before I end, though, here's a thought to consider: when is it right to code a solution, and when is it not? I'll be taking a look at that in the next, and final, article in this miniseries.
The post New Coder: Real World Code Development appeared first on Computer Systems Design.
from Computer Systems Design http://ift.tt/2sAlo2b
0 notes